// Created by wangliang on 2025/2/14.
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_app_demo/pages/image/image_picker_page_view_model.dart';
import 'package:go_router/go_router.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mvvm_plus/mvvm_plus.dart';

class ImagePickerPage extends ViewWidget<ImagePickerPageViewModel> {
  ImagePickerPage({super.key})
      : super(builder: () => ImagePickerPageViewModel());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios),
          onPressed: () {
            context.pop();
          },
        ),
        title: const Text('图片选择器'),
        centerTitle: true,
      ),
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    return Container(
      margin: const EdgeInsets.only(left: 20, top: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ElevatedButton(
            onPressed: () {
              viewModel.pickImage(ImageSource.camera);
            },
            child: const Text('相机'),
          ),
          ElevatedButton(
            onPressed: () {
              viewModel.pickImage(ImageSource.gallery);
            },
            child: const Text('相册'),
          ),
          ElevatedButton(
            onPressed: () {
              viewModel.launchCustomAlbum();
            },
            child: const Text('瑞雪自定义相册'),
          ),
          ElevatedButton(
            onPressed: () {
              viewModel.pickMultiImage();
            },
            child: const Text('相册(选择多张图片)'),
          ),
          ElevatedButton(
            onPressed: () {
              viewModel.upload();
            },
            child: const Text('上传'),
          ),
          Container(
            margin: const EdgeInsets.only(top: 20),
            child: Text(viewModel.progressText),
          ),
          Container(
            margin: const EdgeInsets.only(top: 20),
            child: Wrap(
              spacing: 0,
              runSpacing: 10,
              children: _buildSelectImageWidgets(),
            ),
          )
        ],
      ),
    );
  }

  List<Widget> _buildSelectImageWidgets() {
    List<Widget> list = [];
    final length = viewModel.selectImageList.length;
    for (int i = 0; i < length; i++) {
      list.add(
        Container(
          margin: const EdgeInsets.only(right: 10),
          child: GestureDetector(
            onTap: () {
              viewModel.onClickImage(i);
            },
            child: Image(
              image: FileImage(File(viewModel.selectImageList[i])),
              width: 100,
              height: 100,
              fit: BoxFit.cover,
            ),
          ),
        ),
      );
    }
    return list;
  }
}
